package com.hgj.dao.impl;

import com.hgj.dao.NewsDao;
import com.hgj.entity.News;
import com.hgj.entity.User;
import com.hgj.util.DBUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class NewsDaoImpl implements NewsDao {
    //1.获取连接
    Connection conn=DBUtil.getConnection();
    //2.创建执行sql工具类对象
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(News obj) {
        //3.定义sql语句
        String sql="INSERT INTO  tb_news(news_id,news_title,news_content,news_time,read_nums,news_type) VALUES (?,?,?,?,?,?)";
        //4.传递参数是一个数组 按照占位符顺序传参
        Object[] paras={
                obj.getNews_id(),
                obj.getNews_title(),
                obj.getNews_content(),
                obj.getNews_time(),
                obj.getRead_nums(),
                obj.getNews_type()
        };
        //5.更新到数据库操作
        //update(连接,sql语句,参数)
        try {
            int row=qr.update(conn,sql,paras);
            return  row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(News obj) {
        //3.定义sql语句
        String sql="UPDATE  tb_user SET  user_name=?,user_balance=?,user_point=?,user_sex=?,user_birthday=?,user_phone=?,user_portrait=?,user_password=? WHERE user_id=? ";
        //4.传递参数是一个数组 按照占位符顺序传参
        Object[] paras={   obj.getNews_id(),
                obj.getNews_title(),
                obj.getNews_content(),
                obj.getNews_time(),
                obj.getRead_nums(),
                obj.getNews_type()
        };
        //5.更新到数据库操作
        //update(连接,sql语句,参数)
        try {
            int row=qr.update(conn,sql,paras);
            return  row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql="DELETE FROM tb_news WHERE news_id  = ? ";
        Object[] paras={id};
        try {
            int row=qr.update(conn,sql,paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public News findById(Object id) {
        String sql="select * from tb_news " +
                "  where news_id=?";
        Object[] paras={id};
        try {
            //BeanHandler<类型>(类型.class)
            //转换为类型的对象
            News obj=qr.query(conn,sql,paras,
                    new BeanHandler<News>(News.class));
            return  obj;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findAll() {
        String sql="select * from tb_news";
        Object[] paras={}; //参数
        try {
            //转换器
            //BeanListHandler<类型>(类型.class)
            //将查询结果自动转换为当前类型的List集合
            List<News> newsList=
                    qr.query(conn,sql,paras,
                            new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findSearch(String keyword) {
        String sql="select * from tb_news where news_title like concat(concat('%',?),'%')";
        Object[] paras={keyword};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findPage(int page, int pagesize) {
        String sql="select * from tb_news " +
                "  limit ?,? ";
        int start=(page-1)*pagesize;
        Object[] paras={start,pagesize};
        try {
            List<News> newsList=qr.query(conn,sql,paras,
                    new BeanListHandler<News>(News.class));
            return  newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public long count() {
        String sql="select count(*) from tb_news";
        Object[] paras={};
        try {
            //ScalarHandler<类型>()
            //返回单一数据类型的数值
            //例如Integer Long Float ...
            long nums=qr.query(conn,sql,paras,
                    new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
